package com.zjut.an.util.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipDirectory {
	
	public static void zipDir(String srcDir, String dstZip) {
		ArrayList fileNames = new ArrayList(); // 存放文件名,并非含有路径的名字
		ArrayList files = new ArrayList(); // 存放文件对象		
		
		try{	
		    //指定生成的zip文件名
			FileOutputStream fileOut = new FileOutputStream(dstZip);
		    ZipOutputStream outputStream = new ZipOutputStream(fileOut);
		    outputStream.setEncoding("utf-8");
		    //ZipOutputStream outputStream = new ZipOutputStream(fileOut);
		
		    //指定要压缩的文件夹
		
		    File rootFile = new File(srcDir);
		    listFile(rootFile, fileNames, files); 
		    for (int loop=0; loop<files.size(); loop++){
		    	FileInputStream fileIn = new FileInputStream((File)files.get(loop));
		    	outputStream.putNextEntry(new ZipEntry((String)fileNames.get(loop)));
		    	byte[] buffer = new byte[1024];
		    	while (fileIn.read(buffer) != -1) {
		    		outputStream.write(buffer);
		    	} 
		    	outputStream.closeEntry();
		    	fileIn.close();
		    }
		    outputStream.close();
	   } catch (IOException ioe) {
		   ioe.printStackTrace();
	   }
	}
	
	public static void listFile(File parentFile, ArrayList nameList, ArrayList fileList) throws IOException {
		//String dirname = new String("testapidocs");
						
		if (parentFile.isDirectory()) {
			File[] files = parentFile.listFiles();
			for (int loop=0; loop<files.length; loop++) {
				listFile(files[loop], nameList, fileList);
			}
		}else{
			fileList.add(parentFile);
			
			String filePath = parentFile.getPath();
			String dirName = filePath.substring(filePath.lastIndexOf("\\", filePath.lastIndexOf("\\")-1)+1, filePath.length());
			
			//用parentFile.getName(),只能获取文件的名称，使用getPath()方法获取文件的绝对路径，然后截取成为相对路径
			String parentdir = new String(parentFile.getPath());
			
			nameList.add(dirName);			
		}
	}
	
	
	
	public static void downLoad(String filePath) throws Exception {
		
		HttpServletResponse response = ServletActionContext.getResponse(); 
		System.out.println("filePath:"+filePath);
		File file = new File(filePath); 		
		String filename = file.getName();	//取得文件名	
		filename = URLEncoder.encode(filename, "UTF-8");
		
		response.reset(); // 非常重要 
		response.setContentType("application/octet-stream; charset=utf-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + filename); 
		
		OutputStream outputStream = response.getOutputStream();
		InputStream inputStream = new FileInputStream(filePath);
		
		byte[] buffer = new byte[1024];  
		int i = -1;  
		while ((i = inputStream.read(buffer)) != -1) {  
			outputStream.write(buffer, 0, i);  
		}  
		
		outputStream.flush();  
		outputStream.close();  
		inputStream.close();  
		outputStream = null;  
	}
	
}
